package cn.itcast.web;

import cn.itcast.constant.MessageConstant;
import cn.itcast.result.Result;
import com.example.config.MinioTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
@AllArgsConstructor
public class CommonController {

    private MinioTemplate minioTemplate;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    @SneakyThrows
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传：{}", file);

        String url = minioTemplate.upload(file);

        if (url.isEmpty()) {
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }

        return Result.success(url);
    }
}